/**
 * 文档管理器
 */
export class DocumentManager {
  private static instance: DocumentManager

  private constructor() {} // 私有构造函数确保单例

  /**
   * 获取单例实例
   */
  public static getInstance(): DocumentManager {
    if (!DocumentManager.instance) {
      DocumentManager.instance = new DocumentManager()
    }
    return DocumentManager.instance
  }

  /**
   * 应用/移除加载样式
   * @param loading 是否正在加载
   * @param options 样式选项
   * @param options.target
   * @param options.persist
   */
  applyLoadingStyles(
    loading: boolean,
    options: {
      target?: 'document' | 'body' | 'both'
      persist?: boolean // 是否保持隐藏状态（不移除样式）
    } = { target: 'both', persist: false },
  ): void {
    const { target = 'both', persist = false } = options

    if (loading) {
      if (target === 'document' || target === 'both') {
        this.setDocumentVisibilityStyles()
        this.setDocumentOverflowStyles()
      }
      if (target === 'body' || target === 'both') {
        this.setBodyStyles()
        this.setBodyVisibilityStyles()
      }
    }
    else if (!persist) {
      if (target === 'document' || target === 'both') {
        this.resetDocumentVisibilityStyles()
        this.resetDocumentOverflowStyles()
      }
      if (target === 'body' || target === 'both') {
        this.resetBodyStyles()
        this.resetBodyVisibilityStyles()
      }
    }
  }

  // 设置文档样式
  setDocumentVisibilityStyles(): void {
    const { style } = document.documentElement

    style.setProperty('visibility', 'hidden', 'important')
  }

  setDocumentOverflowStyles(): void {
    const { style } = document.documentElement

    style.setProperty('overflow', 'hidden', 'important')
    style.setProperty('overflow-x', 'hidden', 'important')
    style.setProperty('overflow-y', 'hidden', 'important')
  }

  // 重置文档样式
  resetDocumentVisibilityStyles(): void {
    const { style } = document.documentElement

    style.removeProperty('visibility')
  }

  resetDocumentOverflowStyles(): void {
    const { style } = document.documentElement

    style.removeProperty('overflow')
    style.removeProperty('overflow-x')
    style.removeProperty('overflow-y')
  }

  // 设置 body 样式
  private setBodyStyles(): void {
    if (!document.body)
      return

    const { style } = document.body

    style.setProperty('overflow', 'hidden', 'important')
    style.setProperty('overflow-x', 'hidden', 'important')
    style.setProperty('overflow-y', 'hidden', 'important')
    style.setProperty('height', '100vh', 'important')
    style.setProperty('width', '100vw', 'important')
  }

  private setBodyVisibilityStyles(): void {
    if (!document.body)
      return

    const { style } = document.body

    style.setProperty('visibility', 'hidden', 'important')
    style.setProperty('opacity', '0', 'important')
  }

  // 重置 body 样式
  private resetBodyStyles(): void {
    if (!document.body)
      return

    const { style } = document.body

    style.removeProperty('overflow')
    style.removeProperty('overflow-x')
    style.removeProperty('overflow-y')
    style.removeProperty('height')
    style.removeProperty('width')
  }

  resetBodyVisibilityStyles(): void {
    if (!document.body)
      return

    const { style } = document.body

    style.removeProperty('visibility')
    style.removeProperty('opacity')
  }
}
